﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Data;

namespace Catering.UserTraining.SlrNewStaffTraining
{
    public partial class stafftraining_staff_register : UserIsNull
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int LessonID = CommRequest.GetInt("LessonID", 0);
                this.InitPageInfo(LessonID);
            }
            this.LoadRep();
        }

        /// <summary>
        /// 初始化页面信息
        /// </summary>
        /// <param name="LessonID"></param>
        /// <param name="ClassID"></param>
        public void InitPageInfo(int LessonID)
        {
            Catering_Model.NewStaffLessons _LessonsModel = new Catering_Rules.NewStaffLessons().GetModel(LessonID);
            this.lblLessonName.Text = _LessonsModel.LessonName;
            Catering_Model.ClassInfo _ClassInfoModel = new Catering_Rules.ClassInfo().GetModel(_LessonsModel.ClassID);
            this.lblClassName.Text = _ClassInfoModel.CName;
            this.hf_ClassID.Value = _LessonsModel.ClassID.ToString();
            this.hf_LessosnID.Value = LessonID.ToString();
        }

        //绑定Rep控件
        public void LoadRep()
        {
            string strwhere = " 1=1 AND LessonID=" + this.hf_LessosnID.Value;
            DataTable dt_InfoInfo = new Catering_Rules.NewStaffLessonsCheck().GetList(strwhere + " order by UserID ASC ").Tables[0];
            this.Repeater1.DataSource = dt_InfoInfo;
            this.Repeater1.DataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string[] UserInofo = e.CommandArgument.ToString().Split(',');
            int intLessonID = int.Parse(UserInofo[0]);
            int intUserID = int.Parse(UserInofo[1]);
            if (intUserID <= 0 || intLessonID <= 0) return;
            if (e.CommandName == "del")
            {
                if (new Catering_Rules.NewStaffLessonsCheck().Delete(intLessonID, intUserID))
                {
                    this.LoadRep();
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('删除成功！')", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('删除失败！')", true);
                }
            }
            else if (e.CommandName == "edit")
            {
                ImageButton BtnRegiste = e.Item.FindControl("ImageButton2") as ImageButton;
                Catering_Model.NewStaffLessonsCheck _UserCheckInfo = new Catering_Rules.NewStaffLessonsCheck().GetModel(intLessonID, intUserID);
                _UserCheckInfo.IsRegister = (BtnRegiste.ToolTip == "登记") ? true : false;
                if (new Catering_Rules.NewStaffLessonsCheck().Update(_UserCheckInfo))
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('登记成功！')", true);
                    this.LoadRep();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Affect", "alert('登记失败！')", true);
                }
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ImageButton BtnRegiste = e.Item.FindControl("ImageButton2") as ImageButton;//找到里层的登记按钮
                DataRowView Row = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项 
                BtnRegiste.ToolTip = Row["IsRegister"].ToString() == "False" ? "登记" : "取消登记";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

        }

        protected void btn_Return_Click(object sender, EventArgs e)
        {
            string url = "~/UserTraining/SlrNewStaffTraining/stafftraining_list.aspx";
            Response.Redirect(url);
        }
    }
}